﻿using System;
using System.ServiceModel;

namespace CoreLibrary.Service {

    public class ServiceErrors {

        public static FaultException CreateInvalidRequestException(string message) {
            return CreateInvalidRequestException(message, message);
        }

        public static FaultException CreateInvalidRequestException(string message, string detail) {
            return CreateException(ErrorCode.InvalidRequest, message, detail);
        }

        public static FaultException CreateServerInternalException(string message) {
            return CreateServerInternalException(message, message);
        }

        public static FaultException CreateServerInternalException(string message, string detail) {
            return CreateException(ErrorCode.ServerInternal, message, detail);
        }

        public static FaultException CreateUnauthenticatedException(string message) {
            return CreateUnauthenticatedException(message, message);
        }

        public static FaultException CreateUnauthenticatedException(string message, string detail) {
            return CreateException(ErrorCode.Unauthenticated, message, detail);
        }

        public static FaultException CreateMultipleLoginException(string message) {
            return CreateMultipleLoginException(message, message);
        }

        public static FaultException CreateMultipleLoginException(string message, string detail) {
            return CreateException(ErrorCode.MultipleLogin, message, detail);
        }

        public static FaultException CreateUnauthorisedException(string message) {
            return CreateUnauthorisedException(message, message);
        }

        public static FaultException CreateUnauthorisedException(string message, string detail) {
            return CreateException(ErrorCode.Unauthorised, message, detail);
        }

        public static FaultException CreateAccountLockoutException(string message) {
            return CreateAccountLockoutException(message, message);
        }

        public static FaultException CreateAccountLockoutException(string message, string detail) {
            return CreateException(ErrorCode.AccountLockout, message, detail);
        }

        public static FaultException CreateException(ErrorCode code, string message, string detail) {
            message = Util.GetString(message);
            return new FaultException<ServiceError>(
                new ServiceError(code, message, detail), message);
        }

        public static FaultException<ServiceError> Convert(Exception error) {

            if (error is FaultException<ServiceError>) {
                return (FaultException<ServiceError>)error;
            }

            if (error is FaultException) {
                return new FaultException<ServiceError>(
                       new ServiceError(ErrorCode.ServerInternal, error.Message), error.Message);
            }

            if (error is ArgumentException) {
                return new FaultException<ServiceError>(
                       new ServiceError(ErrorCode.InvalidRequest, error.Message), error.Message);
            }

            if (error is ServerInternalError) {
                return new FaultException<ServiceError>(
                       new ServiceError(ErrorCode.ServerInternal, error.Message), error.Message);
            }

            if (error is InvalidRequestException) {
                return new FaultException<ServiceError>(
                       new ServiceError(ErrorCode.InvalidRequest, error.Message), error.Message);
            }

            if (error is UnauthorisedException) {
                return new FaultException<ServiceError>(
                       new ServiceError(ErrorCode.Unauthorised, error.Message), error.Message);
            }

            return new FaultException<ServiceError>(
                   new ServiceError(ErrorCode.ServerInternal, error.Message), error.Message);

        }
    }
}